package com.wl.lib.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.wl.lib.base.R
import com.wl.lib.base.databinding.ViewListFooterMoreBinding
import com.wl.lib.utils.ResUtils


// Created by wangliang on 2024/4/28.
class LoadMoreFooterView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val binding = ViewListFooterMoreBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }

    var hasMore: Boolean = true

    var loadMore: (() -> Unit)? = null

    var loadMoreState: LoadMoreState =
        LoadMoreState.NORMAL
        set(value) {
            field = value
            when (value) {
                LoadMoreState.NORMAL -> {
                    binding.loadMoreContainer.isVisible = true
                    binding.loadMoreProgressContainer.isInvisible = true
                    if (hasMore) {
                        binding.loadMoreHint.text = ResUtils.getString(R.string.click_to_load_more)
                        binding.loadMoreHint.setOnClickListener {
                            if (loadMore?.invoke() != null) {
                                loadMoreState = LoadMoreState.LOADING
                            }
                        }
                    } else {
                        binding.loadMoreHint.text = ResUtils.getString(R.string.nothing_more)
                        binding.loadMoreHint.setOnClickListener(null)
                    }
                    binding.noMoreLeftIcon.isVisible = !hasMore
                    binding.noMoreRightIcon.isVisible = !hasMore
                }
                LoadMoreState.LOADING -> {
                    binding.loadMoreContainer.isInvisible = true
                    binding.loadMoreProgressContainer.isVisible = true
                }
                LoadMoreState.LOAD_ERROR -> {
                    binding.loadMoreContainer.isVisible = true
                    binding.loadMoreProgressContainer.isInvisible = true
                    binding.loadMoreHint.text = ResUtils.getString(R.string.network_error_click_reload)
                    binding.loadMoreHint.setOnClickListener {
                        if (loadMore?.invoke() != null) {
                            loadMoreState = LoadMoreState.LOADING
                        }
                    }
                }
            }
        }

    var isBound: Boolean = true
        set(value) {
            field = value
            loadMoreMonitor?.let {
                recyclerView?.removeOnScrollListener(it)
                if (value) {
                    recyclerView?.addOnScrollListener(it)
                }
            }
        }
    private var recyclerView: RecyclerView? = null
    private var loadMoreMonitor: RecyclerView.OnScrollListener? = null


    fun setup(recyclerView: RecyclerView) {
        this.recyclerView = recyclerView
        val layoutManager = recyclerView.layoutManager ?: return
        loadMoreMonitor = object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE && layoutManager.childCount > 0) {
                    val count = layoutManager.itemCount
                    val last = (layoutManager.getChildAt(layoutManager.childCount - 1)?.layoutParams
                            as RecyclerView.LayoutParams).viewAdapterPosition
                    @Suppress("MagicNumber")
                    if (count < last + 20 && hasMore) {
                        if (loadMore?.invoke() != null) {
                            loadMoreState = LoadMoreState.LOADING
                        }
                    }
                }
            }
        }
    }

    enum class LoadMoreState {
        NORMAL,
        LOADING,
        LOAD_ERROR;
    }
}